package com.nova.android.tvlauncher.ui.fragment.atv

import android.os.Bundle
import android.view.KeyEvent
import com.nova.android.tvlauncher.AppCache
import com.nova.android.tvlauncher.R
import com.nova.android.tvlauncher.bean.atv_bean.Notification
import com.nova.android.tvlauncher.databinding.FragmentNotificationAtvBinding
import com.nova.android.tvlauncher.databinding.ItemNotificationBinding
import com.nova.android.tvlauncher.ui.activity.atv.StorageGuidedStepActivity
import com.nova.android.tvlauncher.constants.ConstantsAtv
import com_github_lib_base.base.BaseVMFragment
import com_github_lib_base.base.BaseViewModel
import com_github_lib_base.ext.clickNoRepeat
import com_github_lib_base.ext.rv.removeModelAt
import com_github_lib_base.ext.rv.setDifferModels
import com_github_lib_base.ext.rv.setup
import com_github_lib_base.ext.startKtxActivity
import com_github_lib_base.ext.stringValue
import observeLiveEvent

class NotificationATVFragment : BaseVMFragment<FragmentNotificationAtvBinding, BaseViewModel>() {

    override fun initView() {

        observeLiveEvent <Boolean>(ConstantsAtv.UPDATE_STORE_DEVICE_LIST) {
            mBind.rvNotification.setDifferModels(AppCache.notificationList)
            mBind.rvNotification.apply {
                post {
                    layoutManager?.findViewByPosition(0)?.requestFocus()
                }
            }
        }

        handlerRv()

    }

    private fun handlerRv() {
        mBind.rvNotification.apply {
            setup (AppCache.notificationList){
                addType(Notification.StorageDevice::class,R.layout.item_notification)
                onBind {
                    val binding = getBinding<ItemNotificationBinding>()

                    val bean = data
                    when (bean) {
                        is Notification.StorageDevice -> {
                            binding.ivIcon.setImageResource(R.drawable.icon_udisk)
                            binding.tvTitle.text = bean.volumeName
                            binding.tvSubtitle.text = com.nova.lib_lang.R.string.notification_txt_2.stringValue()
                        }
                    }

                    itemView.apply {
                        /*setOnFocusChangeListener { view, b ->
                            //if(!b)binding.tvClose.isSelected = false
                        }*/

                        setOnKeyListener { view, i, keyEvent ->
                            when (keyEvent.keyCode) {
                                KeyEvent.KEYCODE_DPAD_RIGHT -> {
                                    binding.tvClose.isSelected = true
                                    true
                                }

                                KeyEvent.KEYCODE_DPAD_LEFT -> {
                                    binding.tvClose.isSelected = false
                                    false
                                } else -> {
                                false
                            }
                            }
                        }
                        clickNoRepeat {
                            if(binding.tvClose.isSelected) {
                                //删除当前通知
                                mBind.rvNotification.removeModelAt(modelPosition)

                                // 手动设置焦点到下一个或上一个列表项
                                val newFocusPosition =
                                    if (modelPosition < models.size) modelPosition else modelPosition - 1
                                if (newFocusPosition >= 0) {
                                    mBind.rvNotification.apply {
                                        postDelayed({
                                            requestFocus()
                                            layoutManager?.findViewByPosition(newFocusPosition)
                                                ?.requestFocus()
                                        }, 100)
                                    }
                                }
                            }else {
                                //打开通知
                                when (bean) {
                                    is Notification.StorageDevice -> {
                                        /*val jsonString = Json.encodeToString(
                                            Notification.StorageDevice.serializer(),
                                            bean
                                        )*/
                                        AppCache.notification = bean
                                        startKtxActivity<StorageGuidedStepActivity>()
                                    }
                                }
                            }
                        }
                    }
                }
            }

        }
    }

    companion object {
        @JvmStatic
        fun newInstance() =
            NotificationATVFragment().apply {
                arguments = Bundle().apply {

                }
            }
    }

}